<template>
  <div>
    <el-dialog
      :title="updataId ? '修改目录' : '新增目录'"
      :visible="directorysVisible"
      width="400px"
      @close="closed"
      class="subjects-add"
    >
      <el-form
        :rules="rules"
        label-width="80px"
        :model="updataList"
        ref="clearRormRef"
      >
        <el-form-item
          label="所属学科"
          prop="subjectID"
          v-if="!this.directorysId"
        >
          <el-select v-model="updataList.subjectID" placeholder="请选择">
            <el-option
              v-for="(item, index) in options"
              :value="item.value"
              :key="index"
              :label="item.label"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="目录名称" prop="directoryName">
          <el-input
            placeholder="请输入目录名称"
            v-model.trim="updataList.directoryName"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="closed">取 消</el-button>
        <el-button type="primary" @click="subjectClick">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { add, detail, updateById } from '@/api/hmmm/directorys'
import { simpleSubjectsList } from '@/api/hmmm/subjects'

export default {
  name: '',
  components: {},
  props: {
    directorysVisible: {
      type: Boolean,
      required: true
    },
    updataId: {
      type: Number || String
    },
    directorysId: {
      type: Number || Object
    }
  },
  data() {
    return {
      title: '',
      updataList: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      },
      options: []
    }
  },
  computed: {},
  watch: {},
  methods: {
    async getSimpleSubjectsList() {
      const res = await simpleSubjectsList()
      // console.log(res)
      this.options = res.data
    },

    closed() {
      this.$refs.clearRormRef.resetFields()
      this.$emit('update:directorysVisible', false)
      setTimeout(() => {
        this.$emit('update:updataId', 0)
      }, 500)
    },

    async updateSubject(id) {
      const res = await detail(id)
      // console.log(res)
      this.updataList = res.data
    },

    async subjectClick() {
      try {
        await this.$refs.clearRormRef.validate()

        if (this.directorysId) {
          this.updataList.subjectID = this.directorysId
        }

        const update = {
          id: this.updataId,
          subjectID: this.updataList.subjectID,
          directoryName: this.updataList.directoryName
        }

        if (this.updataId) {
          await updateById(update)
        } else {
          await add(this.updataList)
        }

        this.$refs.clearRormRef.resetFields()
        this.$emit('update:directorysVisible', false)
        this.$emit('directorysAdd')
      } catch (error) {
        this.$message.error('添加数据失败')
      }
    }
  },
  created() {},
  mounted() {
    this.getSimpleSubjectsList()
  }
}
</script>
<style scoped lang="scss">
.el-form {
  width: 400px;
  .el-input el-input--medium,
  .el-input__inner,
  .el-input,
  .el-select {
    width: 280px;
  }
}
</style>
